 /*
 
 File: OpenGLScreenReader.m
 
 Abstract: OpenGLScreenReader class implementation. Contains
            OpenGL code which creates a full-screen OpenGL context
            to use for rendering, then calls glReadPixels to read the 
            actual screen bits.
 
 Version: 1.0
 
 Disclaimer: IMPORTANT:  This Apple software is supplied to you by 
 Apple Inc. ("Apple") in consideration of your agreement to the
 following terms, and your use, installation, modification or
 redistribution of this Apple software constitutes acceptance of these
 terms.  If you do not agree with these terms, please do not use,
 install, modify or redistribute this Apple software.
 
 In consideration of your agreement to abide by the following terms, and
 subject to these terms, Apple grants you a personal, non-exclusive
 license, under Apple's copyrights in this original Apple software (the
 "Apple Software"), to use, reproduce, modify and redistribute the Apple
 Software, with or without modifications, in source and/or binary forms;
 provided that if you redistribute the Apple Software in its entirety and
 without modifications, you must retain this notice and the following
 text and disclaimers in all such redistributions of the Apple Software. 
 Neither the name, trademarks, service marks or logos of Apple Inc. 
 may be used to endorse or promote products derived from the Apple
 Software without specific prior written permission from Apple.  Except
 as expressly stated in this notice, no other rights or licenses, express
 or implied, are granted by Apple herein, including but not limited to
 any patent rights that may be infringed by your derivative works or by
 other works in which the Apple Software may be incorporated.
 
 The Apple Software is provided by Apple on an "AS IS" basis.  APPLE
 MAKES NO WARRANTIES, EXPRESS OR IMPLIED, INCLUDING WITHOUT LIMITATION
 THE IMPLIED WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY AND FITNESS
 FOR A PARTICULAR PURPOSE, REGARDING THE APPLE SOFTWARE OR ITS USE AND
 OPERATION ALONE OR IN COMBINATION WITH YOUR PRODUCTS.
 
 IN NO EVENT SHALL APPLE BE LIABLE FOR ANY SPECIAL, INDIRECT, INCIDENTAL
 OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 INTERRUPTION) ARISING IN ANY WAY OUT OF THE USE, REPRODUCTION,
 MODIFICATION AND/OR DISTRIBUTION OF THE APPLE SOFTWARE, HOWEVER CAUSED
 AND WHETHER UNDER THEORY OF CONTRACT, TORT (INCLUDING NEGLIGENCE),
 STRICT LIABILITY OR OTHERWISE, EVEN IF APPLE HAS BEEN ADVISED OF THE
 POSSIBILITY OF SUCH DAMAGE.
 
 Copyright (C) 2007 Apple Inc. All Rights Reserved.
 
 */ 
 
#import "OpenGLScreenReader.h"
#include "OpenGLUtils.h"
#include "DisplayUtils.h"


@implementation OpenGLScreenReader

#pragma mark ---------- Initialization ----------

-(id) init
{
    if (self = [super init])
    {
        // Create a full-screen OpenGL graphics context
        mGLContext = [OpenGLUtils makeFullScreenGLGraphicsContext];
        if (!mGLContext)
        {
            [self release];
            return nil;
        }
        [mGLContext retain];

        // Set our context as the current OpenGL context
        [mGLContext makeCurrentContext];
        // Set full-screen mode
        [mGLContext setFullScreen];

    }
    return self;
}

#pragma mark ---------- Screen Reader  ----------

// Perform a simple, synchronous full-screen read operation using glReadPixels(). 
// Although this is not the most optimal technique, it is sufficient for doing 
// simple one-shot screen grabs.
- (void) readFullScreenToBuffer: (void *) baseAddress bufferBytesPerRow:(size_t) bytesPerRow
{
    long displayWidth, displayHeight;
    [DisplayUtils getMainDisplayDimensions:&displayWidth displayHeight:&displayHeight];

    [self readPartialScreenToBuffer: displayWidth bufferHeight:displayHeight bufferBaseAddress: baseAddress bufferBytesPerRow:bytesPerRow];
}

// Use this routine to read only a portion of the screen pixels
- (void) readPartialScreenToBuffer: (size_t) width bufferHeight:(size_t) height bufferBaseAddress: (void *) baseAddress bufferBytesPerRow:(size_t) bytesPerRow
{
    // select front buffer as our source for pixel data
    glReadBuffer(GL_FRONT);
    
    //Read OpenGL context pixels directly.

    // For extra safety, save & restore OpenGL states that are changed
    glPushClientAttrib(GL_CLIENT_PIXEL_STORE_BIT);
    
    glPixelStorei(GL_PACK_ALIGNMENT, 4); /* Force 4-byte alignment */
    glPixelStorei(GL_PACK_ROW_LENGTH, 0);
    glPixelStorei(GL_PACK_SKIP_ROWS, 0);
    glPixelStorei(GL_PACK_SKIP_PIXELS, 0);
    
    //Read a block of pixels from the frame buffer
    glReadPixels(0, 0, width, height, GL_BGRA, 
    /*
    IMPORTANT
    
    For the pixel data format and type parameters you should *always* specify:
    
        format: GL_BGRA
        type: GL_UNSIGNED_INT_8_8_8_8_REV
    
    because this is the native format of the GPU for both PPC and Intel and will 
    give you the best performance. Any deviation from this format will not give 
    you optimal performance!
    
    BACKGROUND
    
    When using GL_UNSIGNED_INT_8_8_8_8_REV, the OpenGL implementation 
    expects to find data in byte order ARGB on big-endian systems, but BGRA on 
    little-endian systems. Because there is no explicit way in OpenGL to specify 
    a byte order of ARGB with 32-bit or 16-bit packed pixels (which are common
    image formats on Macintosh PowerPC computers), many applications specify 
    GL_BGRA with GL_UNSIGNED_INT_8_8_8_8_REV. This practice works on a 
    big-endian system such as PowerPC, but the format is interpreted differently 
    on a little-endian system, and causes images to be rendered with incorrect colors.

    To prevent images from being rendered incorrectly by this application on little 
    endian systems, you must specify the ordering of the data (big/little endian)
    when creating Quartz bitmap contexts using the CGBitmapContextCreate function. 
    See the createRGBImageFromBufferData: method in the Buffer.m source file for
    the details.

    Also, if you need to reverse endianness, consider using vImage after the read. See: 

    http://developer.apple.com/documentation/Performance/Conceptual/vImage/
    
    */
            GL_UNSIGNED_INT_8_8_8_8_REV,
            baseAddress);

    glPopClientAttrib();

    //Check for OpenGL errors
    GLenum theError = GL_NO_ERROR;
    theError = glGetError();
    NSAssert1( theError == GL_NO_ERROR, @"OpenGL error 0x%04X", theError);
}

#pragma mark ---------- Cleanup  ----------

-(void)dealloc
{    
    // Get rid of GL context
    [NSOpenGLContext clearCurrentContext];
    // disassociate from full screen
    [mGLContext clearDrawable];
    // and release the context
    [mGLContext release];    

    [super dealloc];
}

@end
